﻿Ext.define('yzmh.view.Login',{
    extend:'Ext.window.Window',
    alias: 'widget.loginForm',
    requires: ['Ext.form.*','yzmh.view.CheckCode'],
    initComponent:function(){
        var form = Ext.widget('form', {
            border: false,
            bodyPadding: 10,
            fieldDefaults: {
                labelAlign: 'left',
                labelWidth: 55,
                labelStyle: 'font-weight:bold'
            },
            defaults: {
                margins: '0 0 10 0',
                listeners: {
                    specialkey: function (field, event) {
                        if (event.getKey() == event.ENTER) {
                            field.up('form').onLogin();
                        }
                    }
                }
            },
            items: [{
                xtype: 'textfield',
                fieldLabel: '用户名',
                blankText: '用户名不能为空',
                name: 'UserName',
                id: 'UserName',
                allowBlank: false,
                width: 240
            }, {
                xtype: 'textfield',
                fieldLabel: '密   码',
                allowBlank: false,
                blankText: '密码不能为空',
                name: 'PassWord',
                id: 'PassWord',
                width: 240,
                inputType: 'password'
            }, {
                xtype:'checkcode',
                cls: 'key',
                fieldLabel: '验证码',
                name: 'CheckCode',
                id: 'CheckCode',
                allowBlank: false,
                isLoader: true,
                blankText: '验证码不能为空',
                codeUrl: '/code.ashx',
                width: 160
            }],
            buttons: [{
                text: '登录',
                handler: function () {
                    this.up("form").onLogin();
                }
            }],
            onLogin: function () {
                var form = this.getForm();
                var win = this.up('window');
                if (form.isValid()) {
                    form.submit({
                        clientValidation: true,
                        waitMsg: '请稍后',
                        waitTitle: '正在验证登录',
                        url: 'data/login.aspx?a=login',
                        success: function (form, action) {
                            //登录成功后。
                            //隐藏登录窗口，并重新加载菜单
                            win.hide();
                            location.href = "Default.aspx";
                        },
                        failure: function (form, action) {
                            Ext.MessageBox.show({
                                width: 150,
                                title: "登录失败",
                                buttons: Ext.MessageBox.OK,
                                msg: action.result.errorMessage
                            })
                        }
                    });
                }
            }
        });

        Ext.apply(this,{
            height: 160,
            width: 280,
            title: '用户登陆',
            closeAction: 'hide',
            closable : false, 
            iconCls: 'win',
            layout: 'fit',
            modal : true, 
            plain : true,
            resizable: false,
            items:form
        });
        this.callParent(arguments);
    }
});